<?php
include("game/header.php");

session_start();
include("game/connect.php");

if(!isset($_SESSION['login'])||$_SESSION['login']=="")
{
  # nepodlogininnij, probujem zaloginitsa ili risujem login i parol
  $login = "";
  $pass = "";
  if(isset($_POST['login'])&&isset($_POST['pass']))
  {
    $login = mysql_real_escape_string($_POST['login']);
    $pass = mysql_real_escape_string($_POST['pass']);
    $sql_logins = "select * from tt_users where tt_users.login = '".$login."' and tt_users.pass = '".$pass."'";
    $tt_logins = mysql_query($sql_logins);
    print mysql_error(); 
    while($tt_login = mysql_fetch_array($tt_logins))
    {
      $_SESSION['login'] = $tt_login['login'];
      $_SESSION['userid'] = $tt_login['id'];
      $_SESSION['home'] = "games";
      header('Location: index.php');
      exit();
    }
  }
  # logi ne proshjol, zaprashivajem zanovo
  print "
  <FORM METHOD='POST' ACTION='index.php'>
  <INPUT NAME='login' SIZE='10' TYPE='TEXT' VALUE=".$login.">  
  <BR>
  <INPUT NAME='pass' SIZE='10' TYPE='PASSWORD'>  
  <BR>
  <INPUT NAME='submit' SIZE='10' TYPE='SUBMIT'>  
  </FORM>";
}
else
{
  if(!isset($_SESSION['home']))
  {
    # ne ustanovlenna starnica glavnaja, pereloginivajemsa
    $_SESSION['login'] = "";
    $_SESSION['userid'] = "";
    $_SESSION['home'] = "";
    header('Location: index.php');
    exit();
  }
  else
  {
    print "<FORM NAME='HOME_FORM' METHOD='POST' ACTION='index.php'><INPUT TYPE='hidden' NAME='HOME_FIELD' VALUE=''><INPUT TYPE='HIDDEN' NAME='HOME_ID' VALUE=''></FORM>";
    print "<A onmouseover='this.style.cursor=\"hand\"' onclick='document.HOME_FORM.HOME_FIELD.value=\"games\"; document.HOME_FORM.submit()'>Игры</A> || 
           <A onmouseover='this.style.cursor=\"hand\"' onclick='document.HOME_FORM.HOME_FIELD.value=\"new\"; document.HOME_FORM.submit()'>Новая игра</A> || 
           <A onmouseover='this.style.cursor=\"hand\"' onclick='document.HOME_FORM.HOME_FIELD.value=\"archive\"; document.HOME_FORM.submit()'>Архив</A> || 
           <A onmouseover='this.style.cursor=\"hand\"' onclick='document.HOME_FORM.HOME_FIELD.value=\"profile\"; document.HOME_FORM.submit()'>Правила игры</A> ||
           <A onmouseover='this.style.cursor=\"hand\"' onclick='document.HOME_FORM.HOME_FIELD.value=\"navigator\"; document.HOME_FORM.submit()'>Навигатор карт</A> ||
           <A onmouseover='this.style.cursor=\"hand\"' onclick='document.HOME_FORM.HOME_FIELD.value=\"exit\"; document.HOME_FORM.submit()'>Выход</A>
           <BR>";
    if(isset($_POST['HOME_FIELD']))
    {
      $_SESSION['home'] = $_POST['HOME_FIELD']; 
    }
    
    if($_SESSION['home']=="exit")
    {
      $_SESSION['login'] = "";
      $_SESSION['userid'] = "";
      $_SESSION['home'] = "";
      header('Location: index.php');
      exit();
    }
    elseif($_SESSION['home']=="new")
    {
      include("game/new.php");
    }
    elseif($_SESSION['home']=="games")
    {
      include("game/games.php");
    }
    elseif($_SESSION['home']=="navigator")
    {
      include("game/cards.php");
    }
    elseif($_SESSION['home']=="profile")
    {
      include("tt_rules.htm");
    }
  }
}
include("game/footer.php");
?>
